<template>
	<view class="">
		<view style="display: flex; justify-content: center; font-size: 30rpx;" v-if="false">
			<view>昵称、性别、城市半年内仅可设置一次哦，请谨慎修改</view>
		</view>
		<view class="body">
			<view class="body row">
				<view class="body letf-text" @click="show=true">昵称</view>
				<view class="right">
					<view class="body right-text">{{userInfo.username}}</view>
				</view>
			</view>
			<u-line />
			<view class="body row">
				<view class="body letf-text">性别</view>
				<view class="right" v-if="userInfo.gender==0">
					<picker mode="selector" @change="bindPickerChange" :value="userInfo.gender" :range="genderArr"
						range-key="name" :disabled="false">
						<view style="display: flex; justify-content: center; align-items: center;">
							<view class="body right-text">未知</view>
							<!-- <uni-icons name="arrow-right" color="#8d8c8c" size="24"></uni-icons> -->
							<uni-icons type="right" size="18" color="#8d8c8c"></uni-icons>
						</view>
					</picker>
				</view>
				<view class="right" v-else>
					<view style="display: flex; justify-content: center; align-items: center;">
						<view class="body right-text" v-if="userInfo.gender==1">男</view>
						<view class="body right-text" v-else>女</view>
					</view>
				</view>
			</view>
			<u-line />
			<view class="body row">
				<view class="body letf-text">城市</view>
				<view class="right" @click="showAddress=true">
					<view style="display: flex; justify-content: center; align-items: center;">
						<view class="body right-text">{{userInfo.region}}</view>
						<uni-icons type="right" size="18" color="#8d8c8c"></uni-icons>
					</view>
				</view>
			</view>
			
			<view v-if="showAddress">
				<t-index-address @select="select" :show="false"></t-index-address>
			</view>
		</view>
		<view class="btn">
			<u-button text="退出登录" shape="circle" type="primary" @click="confirm"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		logout,
		setUserAddress,
		setUserInfo
	} from "../../api/login";
	export default {
		
		data() {
			return {
				userInfo: {
					userId: 414570,
					username: '微信用户',
					region: '未设置',
					avatar: "../../static/logo.png",
					gender: 0, //用户性别，0: 未知；1:男性；2:女性
					policy: false,
				},
				genderArr: [{
						name: '男',
						value: 1
					},
					{
						name: '女',
						value: 2
					}
				],
				showAddress: false
			}
		},
		onLoad(option) {
			let info = JSON.parse(decodeURIComponent(option.item));
			if (info) {
				this.userInfo.userId = info.userId
				this.userInfo.username = info.username
				this.userInfo.region = info.region
				this.userInfo.avatar = info.avatar
				this.userInfo.gender = info.gender
				this.userInfo.policy = info.policy
			}
			uni.$on('setRegion', (obj) => {
				this.userInfo.region = obj.region
			})
		},
		onUnload() {
			uni.$off('setRegion')
		},
		onReady() {},
		onShow() {
		},
		methods: {
			bindPickerChange(e) {
				console.log(this.userInfo)
				console.log('picker发送选择改变，携带值为', e.detail.value)
				let index = e.detail.value
				let that = this
				uni.showModal({
					title: '提示',
					content: '性别设置后不可再更改哦',
					success: function(res) {
						if (res.confirm) {
							that.userInfo.gender = that.genderArr[index].value
							setUserInfo({
								id: uni.getStorageSync("userId"),
								username: that.userInfo.username,
								avatar: that.userInfo.avatar,
								gender: that.userInfo.gender
							}).then(res => {
								uni.setStorageSync("userInfo", res.data)
								// that.connectionGoEasy(uni.getStorageSync('uuid'), that.userInfo.avatar, that.userInfo.username)
								console.log("上报成功")
							}).catch(err => {
								console.log("上报失败", err)
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

			select(data) {
				let region = data.province + "-" + data.name
				setUserAddress({
					id: uni.getStorageSync("userId"),
					region: region,
					province_code: data.provinceCode,
					city_code: data.code
				}).then(res => {
					this.userInfo.region = data.province + "-" + data.name
					uni.setStorageSync("userInfo", res.data)
					uni.showToast({
						title: "设置成功",
						icon: "success",
						duration: 1000
					})
				}).catch(err => {
					uni.showToast({
						title: err.msg,
						icon: "none",
						duration: 1000
					})
				})
				this.showAddress = false
			},
			confirm() {
				let that = this
				uni.showModal({
					title: '提示',
					content: '是否要退出登录',
					success: function(res) {
						if (res.confirm) {
							that.getLogout()
						}
					}
				})
				
			},
			getLogout() {
				setTimeout(()=> {
					uni.reLaunch({
						url: "/pages/userHome/userHome",
						success: function(res) {
							uni.clearStorageSync();
						}
					});
				}, 500)
			},
			userNameInput(e) {
				this.userInfo.username = e.detail.value
			}

		}
	}
</script>

<style lang="scss" scoped>
	.body {
		background-color: #fff;
		padding: 10rpx;
		border-radius: 16rpx;

		.row {
			display: flex;
			justify-content: space-between;
		}

		.letf-text {
			padding-left: 20rpx;
			font-size: 30rpx;
		}

		.right {
			display: flex;
			align-items: center;
		}

		.right-text {
			font-size: 30rpx;
		}

		.icon {
			margin-left: 10rpx
		}
	}

	.title {
		font-size: 14px;
		font-weight: bold;
		margin: 20px 0 5px 0;
	}

	.picker {
		padding: 10px;
		background-color: #fff;
		text-align: center;
	}
	.btn {
		margin-top: 40rpx;
		padding: 40rpx;
	}
</style>